/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumEvenGrandparent(TreeNode* root) {
        int ans = 0;

        auto dfs = [&](this auto&& dfs, int g_val, int p_val, TreeNode* node)
        {
            if(node == nullptr) return;

            if((g_val & 1) == 0) 
                ans += node->val;
            
            dfs(p_val, node->val, node->left);
            dfs(p_val, node->val, node->right);
        };

        dfs(1, 1, root);
        return ans;
    }

};
